<?php
namespace app\data\model;
use app\index\exception\LogicException;
use think\admin\Model;

/**
 * @name:门票详情
 * @author:Sam
 * @date:2024-04-10
 * @param:[string] param [desc]
*/
class ActTicketDetails extends Model
{
    protected $table = 'act_ticket_details';

    //售卖状态
    public function getSaleStatusAttr()
    {
        $now = time();
        $begin=strtotime($this->ticketModel->sale_begin);
        $end = strtotime($this->ticketModel->sale_end);
        if($now < $begin){
            //未开始
            return 4;
        }elseif($now > $end){
            //已结束
            return 2;
        }else{
            if($this->rest_quantity=0){
                //已售完
                return 3;
            }else{
                //售卖中
                return 1;
            }
        }

    }

    public function orders(){
        return $this->hasMany(ActOrder::class,'ticket_id');
    }

    public function activityModel()
    {
        return $this->belongsTo(ActActivity::class,'activity_id');
    }

    public function ticketModel()
    {
        return $this->belongsTo(ActActivityTicket::class,'ticket_id');
    }

    public function ifOnSale()
    {
        $now = time();
        $begin=strtotime($this->ticketModel->sale_begin);
        $end = strtotime($this->ticketModel->sale_end);
        return $now>=$begin&&$now<=$end;
    }

    public function saleOneTicket()
    {
        if($this->rest_quantity<=0){
            throw new LogicException('门票已售完！');
        }
        $this->rest_quantity--;
        return $this->save();

    }

}